package tests;

import static org.junit.Assert.*;

import java.util.List;

import model.filemanager.FileManager;
import model.filemanager.MediaFile;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

public class MediaFileTests {

	FileManager fm;
	MediaFile mf;
	int size = 300;
	boolean crop = true;
	BlobKey key = new BlobKey("key");
	String user = "user";
	String name = "file.jpg";
	String type = "image";
	long size2 = 1000000;
	Integer[] totalCount = new Integer[1];
	
	
	@Before
	public void createFile() {
        helper.setUp();
		fm = new FileManager("atestname");
		fm.SaveFile(key, name, type, size2);
	}
	
   private final LocalServiceTestHelper helper =
            new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());


   @After
   public void tearDown() {
   helper.tearDown();
   }

	@Test (expected=EntityNotFoundException.class)
	public void testNullConstructor() throws EntityNotFoundException {
		 mf = new MediaFile(null);
		
	}
	
	@Test 
	public void testConstructor() throws EntityNotFoundException {
		Entity entity = new Entity("entity");
		mf = new MediaFile(entity);
		
		assertTrue("mf is not a MediaFile", mf instanceof MediaFile);
	}
	
	@Test
	public void testgetName() throws EntityNotFoundException {
		List<MediaFile> mediaFiles = fm.SearchForFiles(null, null, null, null, -1, false, 0, 10, totalCount);
		mf = mediaFiles.get(0);
		assertEquals("file.jpg", mf.getName());
	}
	
	@Test
	public void testgetKey() throws EntityNotFoundException {
		List<MediaFile> mediaFiles = fm.SearchForFiles(null, null, null, null, -1, false, 0, 10, totalCount);
		mf = mediaFiles.get(0);
		assertEquals("key", mf.getKey());
	}
	
	@Test
	public void testgetPropertyNull() throws EntityNotFoundException {
		List<MediaFile> mediaFiles = fm.SearchForFiles(null, null, null, null, -1, false, 0, 10, totalCount);
		mf = mediaFiles.get(0);
		
		mf.getProperty(null);
	}
	
	@Test
	public void testgetPropertyNormal() throws EntityNotFoundException {
		List<MediaFile> mediaFiles = fm.SearchForFiles(null, null, null, null, -1, false, 0, 10, totalCount);
		mf = mediaFiles.get(0);
		assertEquals(type, mf.getProperty("type"));
		
	}
	
	@Test
	public void testsetPropertyNull() throws EntityNotFoundException {
		List<MediaFile> mediaFiles = fm.SearchForFiles(null, null, null, null, -1, false, 0, 10, totalCount);
		mf = mediaFiles.get(0);
		
		mf.setProperty(null, "value");		
	}

	@Test
	public void testsetPropertyNormal() throws Exception {
		List<MediaFile> mediaFiles = fm.SearchForFiles(null, null, null, null, -1, false, 0, 10, totalCount);
		mf = mediaFiles.get(0);
		mf.setProperty("name", "media.jpg");
		assertEquals("media.jpg", mf.getName());
	}
	
	@Test
	public void testgetEntity() throws Exception {
		Entity entity = new Entity("entity");
		mf = new MediaFile(entity);
		assertEquals(entity, mf.getEntity());
	}
	
}
